<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="SKU" prop="skus" v-if="this.active !== 'ShopTable'">
                    <el-input v-model.trim="queryParams.skus" placeholder="请输入SKU" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="SPU" prop="spu" v-if="this.active == 'ShopTable'">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable style="width: 240px" />
                </el-form-item>
                <el-form-item label="下单时间" prop="createtime">
                    <el-date-picker v-model="queryParams.createtime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>

                <el-form-item v-if="this.active == 'OrderTable'" label="售后原因" prop="ReasonForRefund">
                        <el-select 
                                v-model="queryParams.ReasonForRefund"
                                filterable
                                multiple
                                collapse-tags
                                clearable
                                placeholder="请选择售后原因">
                            <el-option
                            v-for="item in dict.questionList"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id">
                            </el-option>
                        </el-select>
                </el-form-item>
                <el-form-item v-if="this.active == 'OrderTable'" label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in dict.shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>

                <el-form-item v-if="this.active == 'OrderTable'" label="反馈时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>
                
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
            </el-row>

            <div v-if="this.active == 'ShopTable2'">
                <el-row :gutter="10" class="mb8">
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
                <el-row :gutter="10" class="total-warp">
                    <el-col :span="5">总共单量：{{dict.TotalOrderQuantity}}</el-col>
                </el-row>
            </div>
            <el-tabs v-model="active" type="border-card" @tab-click="handleQuery">
                <el-tab-pane v-for="item in tabs" :key="item.value" :label="item.label" :name="item.value">
                    <component :is="item.value" :tableData="tableData" :query-params="queryParams" :loading="loading"></component>
                </el-tab-pane>
                <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
            </el-tabs>
        </div>
    </div>
</template>

<script>
import { exportFile } from '@/utils/index'
import { queryProductFeedbackList, queryStoreFeedbackList, queryAfterSalesFeedbackList, queryOrderFeedbackList ,exportProductFeedback} from '@/api/order'
import { ReasonForRefund} from '@/api/orderSource'
import { getShopList } from '@/api/shop'
import { parseTime } from '@/utils/quickadmin'
import OrderTable from './components/OrderTable'
import ProductTable from './components/ProductTable'
import ShopTable from './components/ShopTable'
import AfterSaleTable from './components/AfterSaleTable'
const Tabs = [
    { label: 'SKU售后量TOP', value: 'ProductTable', request: queryProductFeedbackList },
    { label: '售后类型TOP', value: 'OrderTable', request: queryOrderFeedbackList },
    { label: 'SPU售后量TOP', value: 'ShopTable', request: queryStoreFeedbackList },
    // { label: '售后类型统计', value: 'AfterSaleTable', request: queryAfterSalesFeedbackList },
]
export default {
    name: 'ProductFeedbackList',
    components: { OrderTable, ProductTable, ShopTable, AfterSaleTable },
    data() {
        return {
            download: false,
            active: 'ProductTable',
            tabs: Tabs,
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 查询参数
            queryParams: {
                order_id: undefined,
                skus: undefined,
                spu: undefined,
                tag_source: undefined,
                tag_campaign: undefined,
                createtime: [
                    parseTime(new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000)),
                    parseTime(new Date()),
                ],
                keyword: undefined,
                shop_id: undefined,
                create_time: undefined,
                ReasonForRefund: undefined,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            dict: {
                shopList: [],
                questionList: [],
                TotalOrderQuantity: 0,
            }
        }
    },
    async mounted() {
        try {
            const questionData = await ReasonForRefund()
            this.dict.questionList = questionData ?? []
        } catch (error) {
            console.log(error)
        }
        this.getList()
    },
    async created() {
        this.loading = true
        try {
            const result = await getShopList()
            this.dict.shopList = result?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
    },
    methods: {
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            qyparams.active = this.active
            exportProductFeedback(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
        jumpReason(row) {
            this.$router.push({
                path: '/production/feedback/afterSales',
                query: {
                    skus: row.skus,
                },
            })
        },
        //列表
        getList() {
            this.tableData = []
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            
            const request = Tabs.find(item=>item.value === this.active).request
            request(qyparams)
                .then(res => {
                    if (res.data.list) {
                        this.tableData = res.data.list

                        //总单量
                        // this.dict.TotalOrderQuantity = res.data.TotalOrderQuantity ?? 0
                        
                    } else {
                        // this.tableData = res.data.map((item, index) => {
                        //     item.rowKey = `${item.skus || item.shop_id}--${index}`
                        //     if (item.children) {
                        //         item.children.map((pre, i) => {
                        //             pre.rowKey = `${item.skus || item.shop_id}--${index}--${i}`
                        //         })
                        //     }
                        //     return { ...item }
                        // })
                    }

                    this.total = res.data?.total ?? 0
                })
                .finally(() => {
                    this.loading = false
                })
        },
        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1

            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>

<style>

.total-warp {
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0 16px 0;
}
</style>
